package com.jhws.sys.appUser.api;

import java.util.List;

import javax.annotation.Resource;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.sys.appUser.bo.AccountInfo;
import com.jhws.sys.appUser.bo.TblSipAcountDimBo;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jhws.sys.appUser.bo.TblSipAcountBindings;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.appUser.bo.SettingInfo;
import com.jhws.sys.appUser.service.ITblSipAcountDimService;

@Controller
@RequestMapping("/setting")
public class SettingAPI {

	@Resource
	protected ITblSipAcountDimService IAppAccountService;
	@Resource
	private OSSClientService ossClientService;

	/**
	 * 免打扰功能
	 */
	@RequestMapping("setDonotDisturb")
	@ResponseBody
	public JsonRespData setDonotDisturb(String username, boolean isDisturb) {
		if (StringUtils.isEmpty(username)) {
			return new JsonRespData("setDonotDisturb", -1, "帐号错误！");
		}
		List<TblSipAcountBindings> list = IAppAccountService.getBindingsByUser(username);
		if (list.size() > 0) {
			for (TblSipAcountBindings tblSipAcountBindings : list) {
				tblSipAcountBindings.setIsCall(!isDisturb);
				IAppAccountService.updateSipBinding(tblSipAcountBindings);
			}
		} else {
			return new JsonRespData("setDonotDisturb", -1, "此帐号还未绑定小区！");
		}
		return new JsonRespData("setDonotDisturb", 1, "设置成功");
	}

	/**
	 * 获取设置信息
	 */
	@RequestMapping("getSettingInfo")
	@ResponseBody
	public JsonRespData getSettingInfo(String username) {
		SettingInfo sInfo = new SettingInfo();
		List<TblSipAcountBindings> list = IAppAccountService.getBindingsByUser(username);
		for (TblSipAcountBindings tblSipAcountBindings : list) {
			if (!tblSipAcountBindings.getIsCall()) {
				sInfo.setDonotDisturb(true);
			}
		}

		return new JsonRespData("getSettingInfo", 1, sInfo);
	}

	@RequestMapping("getAccountInfo")
    @ResponseBody
    public JsonRespData getAccountInfo(String username) {
        TblSipAcountDimBo acount = IAppAccountService.getSipAccountByUser(username);
        AccountInfo accountInfo = new AccountInfo();
        if(StringUtils.isNotEmpty(acount.getHeadpicUrl()))
            accountInfo.setHeadpicUrl(ossClientService.getUrl(acount.getHeadpicUrl(),false));
        else
            accountInfo.setHeadpicUrl("");
        accountInfo.setNickName(acount.getNickName());
        accountInfo.setUsername(acount.getUsername());
        return new JsonRespData("",1,accountInfo);
    }

    /**
     * 更新账号相关配置信息
     * @param username   账号名，一般是手机号
     * @param nickName   账号昵称
     * @param headPicUrl 账号头像地址
     * @return
     * @throws Exception
     */
    @RequestMapping("updateAccountInfo")
    @ResponseBody
    public JsonRespData updateUserInfo(String username,String nickName,String headPicUrl) throws Exception{
        TblSipAcountDimBo sipAcount = IAppAccountService.getSipAccountByUser(username);

        if(StringUtils.isNotEmpty(headPicUrl)){
            String postFix = FilenameUtils.getExtension(headPicUrl);
            String newUrl = "userInfo/"+sipAcount.getUsername()+"/headpic."+postFix;
			ossClientService.gerneralCopyFile(headPicUrl,newUrl);
            sipAcount.setHeadpicUrl(newUrl+"?"+ System.currentTimeMillis());
        }

        if(StringUtils.isNotEmpty(nickName)){
            sipAcount.setNickName(nickName);
        }

        IAppAccountService.save(sipAcount);
        return new JsonRespData("",1,"");
    }
}
